#include "save_file.h"
#include <QDebug>
#include <QDate>
#include <string>
#include <QDir>

save_file::save_file()
{
    QDir path; // 创建一个QDir变量
    if (!path.exists("./Historical data")) { // 使用QDir成员函数exists()来判断文件夹是否存在
    path.mkdir("./Historical data"); // 使用mkdir来创建文件夹
    }
    QDateTime datetime = QDateTime::currentDateTime();
    QString str = datetime.toString("yyyy-MM-dd");
    str.append(".txt");
    filename.append( "./Historical data/");
    this->filename.append(str);
    QFile file(filename);
    if(file.open(QIODevice::ReadWrite| QIODevice::Text))
    {
        QByteArray data = file.readAll();
        //file.close();
        if(data.length() == 0)
        {
            QString str = "传感器重量，五路热电偶温度，加热炉开断情况，五路阀门开端状态，推杆高度，电机转速，加热炉功率";
            QTextStream in(&file);
            in.setCodec("UTF-8");
            in << str;
            in << '\n';
            file.close();
        }
    }
}

void save_file::save(data *shu)
{

    QFile file(this->filename);
    if(file.open(QIODevice::Append | QIODevice::Text))
    {
        QTextStream out(&file);
        out.setCodec("UTF-8");
        QString * in;
        float num;
        float * wen;
        num = shu->get_zhong();
        out << num;
        out << ",";
        wen = shu->get_wen();
        for(int i = 0;i < 5;i++)
        {
               out << wen[i];
               out << "_";
        }
        out << ",";
        in = shu->get_jiare();
        for(int i = 0;i < 5;i++)
        {
               out << in[i];
               out << "_";
        }
        out << ",";
        in = shu->get_famen();
        for(int i = 0;i < 5;i++)
        {
               out << in[i];
               out << "_";
        }
        out << ",";
        num = shu->get_tui();
        out << num;
        out << ",";
        num = shu->get_zhuan();
        out << num;
        out << ",";
        num = shu->get_gong();
        out << num;
        out << '\n';
        file.close();
    }else
    {
     qDebug() << "SAVE_ERROR"<<endl;
    }
}
